﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GovernmentOfficeProject.LevelDataAccessModel;
using GovernmentOfficeProject.LevelBusinessLogic;
using GovernmentOfficeProject.SysTools;

namespace GovernmentOfficeProject.LevelViewUI.information
{
    public partial class InformationLog : System.Web.UI.Page
    {
        decimal dclLoginUserID = -1;
        List<Entity_COM_Sms_Ready> smsReadyList = null;
        List<Entity_COM_Sms_Ready> smsReadyListIsTimeQuantum = null;
        BLL_SmsOperation bllSmsOperation = new BLL_SmsOperation();
        static bool blSelectState = false;  //确定是根据条件true,还是获得全部false,主要用在分页处理.

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限判断
            if (SystemContext.CurrentUserID == 0)
            {
                Response.Redirect("~/Login.htm?FromUrl=" + HttpUtility.UrlEncode(Request.Url.AbsoluteUri));
            }
            else
            {
                SystemContext.CurrentUserID = SystemContext.CurrentUserID;

            }
            dclLoginUserID = SysTools.SystemContext.CurrentUserID;
            if (!Page.IsPostBack)
            {
                smsReadyList = new List<Entity_COM_Sms_Ready>();
                gridviewBind();
            }
        }

        private bool gridviewBind()
        {
            try
            {
                bool flag = false;
                if (smsReadyList.Count >0)
                {
                    gridviewBind(smsReadyList);
                }
                else
                {
                    Entity_COM_Sms_Ready smsReadyParameter = new Entity_COM_Sms_Ready();
                    smsReadyParameter.RdyFromUserID = dclLoginUserID;
                    smsReadyParameter.RdyAccessType = "informSms";
                    smsReadyParameter.RdyAccessValue = 2003;
                    smsReadyList = bllSmsOperation.getSmsReadyListBysmsRdyEntity(smsReadyParameter);
                    this.GridViewSmsLog.DataSource = smsReadyList;
                    this.GridViewSmsLog.DataBind();
                }
                return flag;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }
        private bool gridviewBind(Object objDataSource)
        {
            try
            {
                this.GridViewSmsLog.DataSource = (List<Entity_COM_Sms_Ready>)objDataSource;
                this.GridViewSmsLog.DataBind();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        protected void GridViewSmsLog_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (blSelectState)
            {                
                if (smsReadyListIsTimeQuantum != null)
                {
                    this.GridViewSmsLog.PageIndex = e.NewPageIndex;
                    gridviewBind(smsReadyListIsTimeQuantum);
                }
                else
                {
                    DateTime dtmStart = Convert.ToDateTime(this.txtDateStart.Text.Trim());
                    DateTime dtmEnd = Convert.ToDateTime(this.txtDateEnd.Text.Trim());
                    string strStartTime = dtmStart.ToString("yyyy-MM-dd");
                    string strEndTime = dtmEnd.ToString("yyyy-MM-dd");
                    string strAppendStart = " 00:00:00";
                    string strAppendEnd = " 11:59:59";
                    Entity_COM_Sms_Ready smsReadyParameter = new Entity_COM_Sms_Ready();
                    smsReadyParameter.RdyFromUserID = dclLoginUserID;
                    smsReadyParameter.RdyAccessType = "informSms";
                    smsReadyParameter.RdyAccessValue = 2003;
                    smsReadyListIsTimeQuantum = bllSmsOperation.getSmsListBySmsEntityAndSendTimeStartEnd(smsReadyParameter, strStartTime + strAppendStart, strEndTime + strAppendEnd);
                    this.GridViewSmsLog.PageIndex = e.NewPageIndex;
                    gridviewBind(smsReadyListIsTimeQuantum);
                }
            }
            else
            {                
                if (smsReadyList != null)
                {
                    this.GridViewSmsLog.PageIndex = e.NewPageIndex;
                    gridviewBind(smsReadyList);
                }
                else
                {
                    Entity_COM_Sms_Ready smsReadyParameter = new Entity_COM_Sms_Ready();
                    smsReadyParameter.RdyFromUserID = dclLoginUserID;
                    smsReadyParameter.RdyAccessType = "informSms";
                    smsReadyParameter.RdyAccessValue = 2003;
                    smsReadyList = bllSmsOperation.getSmsReadyListBysmsRdyEntity(smsReadyParameter);
                    this.GridViewSmsLog.PageIndex = e.NewPageIndex;
                    gridviewBind(smsReadyList);
                }
            }
        }

        /// <summary>     
        /// 日期选择图标被点击     
        /// </summary>     
        protected void imgBtnDateStart_Click(object sender, ImageClickEventArgs e)
        {
            //控制日历的显示与隐藏
            divCalendarStart.Visible = !divCalendarStart.Visible;
        }

        /// <summary>     
        /// 日期选择图标被点击     
        /// </summary>     
        protected void imgBtnDateEnd_Click(object sender, ImageClickEventArgs e)
        {
            //控制日历的显示与隐藏         
            this.divCalendarEnd.Visible = !this.divCalendarEnd.Visible;
        }

        /// <summary>     
        /// 选择日期，通过AJAX触发     
        /// </summary>  
        protected void calendarDateStart_SelectionChanged(object sender, EventArgs e)
        {
            this.txtDateStart.Text = this.calendarDateStart.SelectedDate.ToShortDateString();
            // 隐藏日历
            this.divCalendarStart.Visible = false;
            //设置日历下textbox的焦点，方便用户输入。移除或改变下行代码设置为您自己的控件         
            this.txtDateStart.Focus();
            //someTextBox.Focus();    
        }

        /// <summary>     
        /// 选择日期，通过AJAX触发     
        /// </summary>  
        protected void calendarDateEnd_SelectionChanged(object sender, EventArgs e)
        {
            this.txtDateEnd.Text = this.calendarDateEnd.SelectedDate.ToShortDateString();
            // 隐藏日历         
            this.divCalendarEnd.Visible = false;
            //设置日历下textbox的焦点，方便用户输入。移除或改变下行代码设置为您自己的控件         
            //this.txtDateEnd.Focus();
            //someTextBox.Focus();    
        }

        protected void lbtnALL_Click(object sender, EventArgs e)
        {
            blSelectState = false;
            if (smsReadyList!=null)
            {
                this.gridviewBind(smsReadyList);
            }
            else
            {
                Entity_COM_Sms_Ready smsReadyParameter = new Entity_COM_Sms_Ready();
                smsReadyParameter.RdyFromUserID = dclLoginUserID;
                smsReadyParameter.RdyAccessType = "informSms";
                smsReadyParameter.RdyAccessValue = 2003;
                smsReadyList = bllSmsOperation.getSmsReadyListBysmsRdyEntity(smsReadyParameter);
                gridviewBind(smsReadyList);
            }            
        }

        protected void lbtnSelect_Click(object sender, EventArgs e)
        {            
            if (this.txtDateStart.Text == "" || this.txtDateStart.Text == "请选择开始时间→")
            {
                this.txtDateStart.Text = "选择开始时→";
            }
            else if (this.txtDateEnd.Text == "" || this.txtDateEnd.Text == "请选择结束时间→")
            {
                this.txtDateEnd.Text = "选择结束时→";
            }
            else
            {
                blSelectState = true;
                DateTime dtmStart = Convert.ToDateTime(this.txtDateStart.Text.Trim());
                DateTime dtmEnd = Convert.ToDateTime(this.txtDateEnd.Text.Trim());
                string strStartTime = dtmStart.ToString("yyyy-MM-dd");
                string strEndTime = dtmEnd.ToString("yyyy-MM-dd");
                string strAppendStart = " 00:00:00";
                string strAppendEnd = " 11:59:59";
                Entity_COM_Sms_Ready smsReadyParameter = new Entity_COM_Sms_Ready();
                smsReadyParameter.RdyFromUserID = dclLoginUserID;
                smsReadyParameter.RdyAccessType = "informSms";
                smsReadyParameter.RdyAccessValue = 2003;
                smsReadyListIsTimeQuantum = bllSmsOperation.getSmsListBySmsEntityAndSendTimeStartEnd(smsReadyParameter, strStartTime + strAppendStart, strEndTime + strAppendEnd);
                gridviewBind(smsReadyListIsTimeQuantum);
            }
        }
    }
}
